/**
 *******************************************************************************
 * @file  template/source/main.c
 * @brief Main program template for the Device Driver Library.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2022-03-31       CDT             First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2022-2025, Xiaohua Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by XHSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "main.h"

#include "FreeRTOS.h"
#include "task.h"

#include "ethernetif.h"
#include "lwip/tcpip.h"
#include "lwip/api.h"

#include "lwip/apps/lwiperf.h"

#include "bsp_common.h"

#define LED_TOGGLE_PERIOD (100)

/**
 * @addtogroup HC32F4A0_DDL_Examples
 * @{
 */

/**
 * @addtogroup LL_Templates
 * @{
 */

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/


//Static IP ADDRESS
#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 1
#define IP_ADDR3 50
// Netmask
#define NETMASK_ADDR0 255
#define NETMASK_ADDR1 255
#define NETMASK_ADDR2 255
#define NETMASK_ADDR3 0
// Gateway
#define GW_ADDR0 192
#define GW_ADDR1 168
#define GW_ADDR2 1
#define GW_ADDR3 1

struct netif my_netif;

void tcpip_stack_init(void *param)
{
    ip_addr_t ipaddr, netmask, gw;
    tcpip_init(NULL, NULL);
    IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
    IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
    IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
    netif_add(&my_netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
    netif_set_default(&my_netif);

    /*When the netif is fully configured this function must be called*/
    netif_set_up(&my_netif);
    
    xTaskCreate(EthernetIF_CheckLink, "EthernetIF_CheckLink", 128, &my_netif, 1, NULL);
    
    lwiperf_start_tcp_server_default(NULL, NULL);
    DDL_Printf("LWIP INIT OK\r\n");
    
    vTaskDelete(NULL);
}

void led_task(void *param)
{
    while(1)
    {
        GPIO_TogglePins(BSP_LED0_PORT, BSP_LED0_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
        GPIO_TogglePins(BSP_LED1_PORT, BSP_LED1_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
        GPIO_TogglePins(BSP_LED2_PORT, BSP_LED2_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
        GPIO_TogglePins(BSP_LED3_PORT, BSP_LED3_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
    }
    vTaskDelete(NULL);
}

//char pcWriteBuffer[256];

//void vTaskStats(void *pvParameters)
//{
//    while(1) {
//        vTaskGetRunTimeStats(pcWriteBuffer);
//        printf("%s\n", pcWriteBuffer);
//        
//        vTaskDelay(pdMS_TO_TICKS(3000));
//    }
//}

/**
 * @brief  Main function of template project
 * @param  None
 * @retval int32_t return value, if needed
 */
int32_t main(void)
{
    /* MCU Peripheral registers write unprotected. */
    LL_PERIPH_WE(LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_GPIO | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_SRAM);
    
    BSP_CLK_Init();
    
    /* Configure UART */
    DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_Preinit);
    
    BSP_LED_Init();
    BSP_LEDSide_Init();
    
    /* MCU Peripheral registers write protected. */
    LL_PERIPH_WP(LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_SRAM);
    
    xTaskCreate(tcpip_stack_init, "tcpip_stack_init", 256, NULL, 2, NULL);
    xTaskCreate(led_task, "led_task", 128, NULL, 3, NULL);
    // xTaskCreate(vTaskStats, "vTaskStats", 128, NULL, 3, NULL);
    vTaskStartScheduler();
    
    for (;;) {
    }
}

/**
 * @}
 */

/**
 * @}
 */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
